﻿using PermissionManager.Model;
using PermissionsManager.Models;
using PermissionsManager.BLL;
using PermissionsManager.Model;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using PermissionManager.BLL;
using Newtonsoft.Json;
using System;
using PermissionManager.Attributes;
using PermissionManager.Models;

namespace PermissionsManager.Controllers
{
    public class RoleController : Controller
    {

        private RoleService roleService = new RoleService();
        private ModuleService moduleService = new ModuleService();
        private MenuService menuService = new MenuService();
       
        


        /// <summary>
        /// 角色管理
        /// </summary>
        /// <returns></returns>
        public ActionResult RoleCompetence()
        {
            ViewBag.Modules = moduleService.QueryList(a => true);
            return View();
        }
        /// <summary>
        /// 新增角色
        /// </summary>
        /// <returns></returns>
        [Describe("新增角色")]
        public ActionResult Competence()
        {
            ViewBag.Modules = moduleService.QueryList(a => true);
            ViewBag.Funs = menuService.GetFunctionsByAdd();
            return View();
        }
        /// <summary>
        /// 编辑角色
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [Describe("编辑角色")]
        public ActionResult UpdateCompetence(Nullable<int> Id)
        {
            ViewBag.Modules = moduleService.QueryList(a => true);
            ViewBag.RoleId = Id;
            return View();
        }
        /// <summary>
        /// 得到对应角色信息
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public JsonResult GetRole(int Id)
        {
            Role role=roleService.Query(a => a.Id == Id);           
            return Json(role);
        }
        /// <summary>
        /// 得到对应角色的权限信息
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public JsonResult GetFunctionsByUpdate(int Id)
        {
            GetJsonToTable<MenuExtend> getJson = new GetJsonToTable<MenuExtend>()
            {
                data = menuService.GetFunctionsByUpdate(Id)
            };
            return Json(getJson);
        }        

        /// <summary>
        /// 得到角色对应的组
        /// </summary>
        /// <returns></returns>      
        public JsonResult GetModule(int RoleId)
        {
            ModuleRole moduleRole = roleService.GetModule(RoleId);
            Opretation opretation = new Opretation()
            {
                Success = moduleRole != null,
                Message = moduleRole.ModuleId.ToString()
            };
            return Json(opretation);
        }
        // <summary>
        // 得到角色列表
        // </summary>
        // <returns></returns>
        [Describe("查看所有角色")]
        public JsonResult GetRoleUser(int page, int limit)
        {
            List<RoleUserCount> list = roleService.GetRoleUser();
            GetJsonToTable<RoleUserCount> getJson = new GetJsonToTable<RoleUserCount>()
            {
                code = 0,
                msg = "",
                count = list.Count,
                data = list.Skip((page - 1) * limit).Take(limit).ToList()
            };
            return Json(getJson, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 更改角色
        /// </summary>
        /// <param name="role"></param>
        /// <param name="ModuleId"></param>
        /// <returns></returns>
        [Describe("修改角色")]
        public JsonResult UpdateRole(Role role, int ModuleId,string MenuFun)
        {
            List<MenuExtend> menus = JsonConvert.DeserializeObject<List<MenuExtend>>(MenuFun);
            Opretation opretation = new Opretation()
            {
                Success = roleService.UpdateRole(role, ModuleId, menus)
            };
            return Json(opretation);
        }
        /// <summary>
        /// 删除角色
        /// </summary>
        /// <param name="RoleId"></param>
        /// <returns></returns>
        [Describe("删除角色")]
        public JsonResult DelRole(int RoleId)
        {
            Opretation opretation = new Opretation()
            {
                Success = roleService.Delete(RoleId)
            };
            return Json(opretation);
        }
        /// <summary>
        /// 更改角色状态
        /// </summary>
        /// <param name="RoleId"></param>
        /// <param name="IsEnable"></param>
        /// <returns></returns>
        [Describe("修改角色状态")]
        public JsonResult UpdateIsEnableById(int RoleId, bool IsEnable)
        {
            Opretation opretation = new Opretation()
            {
                Success = roleService.UpdateIsEnableById(RoleId, IsEnable)
            };
            return Json(opretation);
        }

        /// <summary>
        /// 新增角色权限
        /// </summary>
        /// <param name="role"></param>
        /// <param name="ModuleId"></param>
        /// <param name="MenuFun"></param>
        /// <returns></returns>
        [Describe("新增角色权限")]
        public JsonResult AddRole(Role role, int ModuleId, string MenuFun)
        {
            //将Json字符串转为对象
            List<MenuFun> menuFunList = JsonConvert.DeserializeObject<List<MenuFun>>(MenuFun);
            Opretation opretation = new Opretation()
            {
                Success = roleService.AddRole(role, ModuleId, menuFunList, AdminUserContext.context.UserInfo_no.Id)
            };
            return Json(opretation);
        }
    }
}